package com.yanqu.road.dao.impl.activity.dunhuangshiku;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.dunhuangshiku.DunHuangShiKuAttackInfo;
import com.yanqu.road.entity.activity.dunhuangshiku.DunHuangShiKuHelpRecordData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Author cwq
 * @Data 2021/4/19 15:55
 */
public class DunHuangShiKuHelpRecordDataDaoImpl extends TempDataObjectDao<DunHuangShiKuHelpRecordData> {
    public static String getDunHuangShiKuHelpRecordDataSql = "select * from t_c_dunhuangshiku_help_record_data where activityId=? and groupId = ?";
    public List<DunHuangShiKuHelpRecordData>  getDunHuangShiKuHelpRecordData(long activityId, int groupId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<DunHuangShiKuHelpRecordData> list = new ArrayList();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getDunHuangShiKuHelpRecordDataSql);
            int i = 1;
            call.setLong(i++,activityId);
            call.setInt(i, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                DunHuangShiKuHelpRecordData dunHuangShiKuHelpRecordData = parse(rs);
                list.add(dunHuangShiKuHelpRecordData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public static String updateDunHuangShiKuHelpRecordDataSql = "update t_c_dunhuangshiku_help_record_data set " +
            "activityId=?, userId=?,eventId=?,targetId=?,targetTotalBlood=?,helpAttackMap=?,killedUserId=?," +
            "isReceived=?,bookNum=?,reward=?,recordTime=?,groupId=? where id=?";
    @Override
    protected boolean updateData(DunHuangShiKuHelpRecordData dunHuangShiKuHelpRecordData) {
        dunHuangShiKuHelpRecordData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, dunHuangShiKuHelpRecordData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, dunHuangShiKuHelpRecordData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, dunHuangShiKuHelpRecordData.getEventId()));
        para.put(i++, new DbParameter(Types.BIGINT, dunHuangShiKuHelpRecordData.getTargetId()));
        para.put(i++, new DbParameter(Types.BIGINT, dunHuangShiKuHelpRecordData.getTargetTotalBlood()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(dunHuangShiKuHelpRecordData.getHelpAttackMap())));
        para.put(i++, new DbParameter(Types.BIGINT, dunHuangShiKuHelpRecordData.getKilledUserId()));
        para.put(i++, new DbParameter(Types.BOOLEAN, dunHuangShiKuHelpRecordData.isReceived()));
        para.put(i++, new DbParameter(Types.INTEGER, dunHuangShiKuHelpRecordData.getBookNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, dunHuangShiKuHelpRecordData.getReward()));
        para.put(i++, new DbParameter(Types.BIGINT, dunHuangShiKuHelpRecordData.getRecordTime()));
        para.put(i++, new DbParameter(Types.INTEGER, dunHuangShiKuHelpRecordData.getGroupId()));
        para.put(i++, new DbParameter(Types.BIGINT, dunHuangShiKuHelpRecordData.getId()));
        if (getDb().executeNoneQuery(updateDunHuangShiKuHelpRecordDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertDunHuangShiKuHelpRecordDataSql = "insert into t_c_dunhuangshiku_help_record_data(id," +
            "activityId,userId,eventId,targetId,targetTotalBlood,helpAttackMap,killedUserId,isReceived,bookNum," +
            "reward,recordTime,groupId) values (?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(DunHuangShiKuHelpRecordData dunHuangShiKuHelpRecordData) {
        dunHuangShiKuHelpRecordData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, dunHuangShiKuHelpRecordData.getId()));
        para.put(i++, new DbParameter(Types.BIGINT, dunHuangShiKuHelpRecordData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, dunHuangShiKuHelpRecordData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, dunHuangShiKuHelpRecordData.getEventId()));
        para.put(i++, new DbParameter(Types.BIGINT, dunHuangShiKuHelpRecordData.getTargetId()));
        para.put(i++, new DbParameter(Types.BIGINT, dunHuangShiKuHelpRecordData.getTargetTotalBlood()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(dunHuangShiKuHelpRecordData.getHelpAttackMap())));
        para.put(i++, new DbParameter(Types.BIGINT, dunHuangShiKuHelpRecordData.getKilledUserId()));
        para.put(i++, new DbParameter(Types.BOOLEAN, dunHuangShiKuHelpRecordData.isReceived()));
        para.put(i++, new DbParameter(Types.INTEGER, dunHuangShiKuHelpRecordData.getBookNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, dunHuangShiKuHelpRecordData.getReward()));
        para.put(i++, new DbParameter(Types.BIGINT, dunHuangShiKuHelpRecordData.getRecordTime()));
        para.put(i, new DbParameter(Types.INTEGER, dunHuangShiKuHelpRecordData.getGroupId()));
        if (getDb().executeNoneQuery(insertDunHuangShiKuHelpRecordDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected DunHuangShiKuHelpRecordData parseData(ResultSet rs) throws SQLException {
        DunHuangShiKuHelpRecordData dunHuangShiKuHelpRecordData = new DunHuangShiKuHelpRecordData();
        dunHuangShiKuHelpRecordData.setId(rs.getLong("id"));
        dunHuangShiKuHelpRecordData.setActivityId(rs.getLong("activityId"));
        dunHuangShiKuHelpRecordData.setUserId(rs.getLong("userId"));
        dunHuangShiKuHelpRecordData.setEventId(rs.getInt("eventId"));
        dunHuangShiKuHelpRecordData.setTargetId(rs.getLong("targetId"));
        dunHuangShiKuHelpRecordData.setTargetTotalBlood(rs.getLong("targetTotalBlood"));
        dunHuangShiKuHelpRecordData.setHelpAttackMap(JSON.parseObject(rs.getString("helpAttackMap"),new TypeReference<Map<Long, DunHuangShiKuAttackInfo>>() {}));
        dunHuangShiKuHelpRecordData.setKilledUserId(rs.getLong("killedUserId"));
        dunHuangShiKuHelpRecordData.setIsReceived(rs.getBoolean("isReceived"));
        dunHuangShiKuHelpRecordData.setBookNum(rs.getInt("bookNum"));
        dunHuangShiKuHelpRecordData.setReward(rs.getString("reward"));
        dunHuangShiKuHelpRecordData.setRecordTime(rs.getLong("recordTime"));
        dunHuangShiKuHelpRecordData.setGroupId(rs.getInt("groupId"));

        return dunHuangShiKuHelpRecordData;
    }

    public Long getMaxRecordId() {
        String sql = "select ifnull(max(id),0) as maxId from t_c_dunhuangshiku_help_record_data";
        ResultSet rs = null;
        CallableStatement call = null;
        long maxId = 0L;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                maxId = rs.getLong("maxId");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return maxId;
    }

}
